package com.boxin.ims.modules.bi.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.framework.base.dao.OpResult;
import com.boxin.ims.modules.wechat.dao.WechatSceneDao;
import com.boxin.ims.modules.wechat.entity.WechatScene;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * @author Administrator
 * @date 2014-3-15
 * @function <p>数据分析Dao类 </p>
 */
@Service
public class BIDao {
	
	@Autowired
	private DaoHelper daoHelper;
	
	@Autowired
	private WechatSceneDao wechatSceneDao;
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-3-15  上午11:48:45
	 * @function <p> 消息交互 统计  </p>
	 * @param userId
	 * @return
	 */
	public List<Map<String,Object>> queryMessageStatInfo(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryMessageStatInfo", params);
	}
	
	
	/**
	 * 查询订阅
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-19下午4:51:02
	 */
	public List<Map<String,Object>> querySubscribeMessage(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.querySubscribeMessage", params);
	}
	
	/**
	 * 查询会员男女
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-25上午9:13:22
	 */
	public List<Map<String,Object>> queryMemberSex(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryMemberSex", params);
	}
	
	
	/**
	 * 查询二维码关注的信息
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-25下午1:38:31
	 */
	public List<Map<String,Object>> queryQrCodeSubscribeMessage(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryQrCodeSubscribeMessage", params);
	}
	
	
	/**
	 * 查询出所有普通信息每天发送的个数
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-29上午11:51:14
	 */
	public List<Map<String,Object>> queryGeneralMessage(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryGeneralMessage", params);
	}
	
	
	/**
	 * 查询出每天通过二维码关注的次数
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-29上午11:51:14
	 */
	public List<Map<String,Object>> querySubscribeMessageByQrCode(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.querySubscribeMessageByQrCode", params);
	}
	/**
	 * 查询会员佣金返利
	 * @author	ziyang
	 * @param params
	 * @return
	 */
	public List<Map<String,Object>> queryMemberCharges(Map<String, Object> params){
		
		return daoHelper.queryForList("com.boxin.ims.bi.queryMemberCharges", params);
	}
	
	/**
	 * 查询某月二维码关注渠道的数量
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-29上午11:51:14
	 */
	public List<Map<String,Object>> querySceneMessageByDay(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.querySceneMessageByDay", params);
	}
	
	
	/**
	 * 插入
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-31下午4:26:43
	 */
	public OpResult insertBiScene(Map<String,Object> params){
		return daoHelper.insert("com.boxin.ims.bi.insertBiScene", params);
	}
	
	/**
	 * 更改
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-3-31下午4:26:43
	 */
	public OpResult updateBiScene(Map<String,Object> params){
		return daoHelper.update("com.boxin.ims.bi.updateBiScene", params);
	}
	
	
	/**
	 * 获得会员增长的信息
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-4-4下午2:33:19
	 */
	public List<Map<String,Object>> queryBiWechatMember(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryBiWechatMember", params);
	}
	
	
	/**
	 * 平台活跃人数
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-4-4下午2:33:19
	 */
	public List<Map<String,Object>> queryBiAliveMember(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryAliveMember", params);
	}
	
	
	/**
	 * 每次通过二维码过来的时候在bi_scene 添加count
	 *@author xiaoqian
	 * 2014-3-31下午3:05:59
	 */
	public void addSceneCount(User user,Map<String,String> map){
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
		String Time=sdf.format(new Date());
		Time=Time.replace("-", "");
		Map<String, Object> params=new HashMap<String, Object>();
		
		
		WechatScene wechatScene=wechatSceneDao.wechatSceneBySceneId(user.getId(),Long.parseLong( map.get("xml.EventKey").substring(8,map.get("xml.EventKey").length() )));
		
		params.put("user_id",user.getId());
		params.put("year",Time.subSequence(0, Time.length()-4));
		params.put("month",Time.subSequence(0, Time.length()-2));
		params.put("day",Time);
		params.put("scene_id",map.get("xml.EventKey").substring(8,map.get("xml.EventKey").length()));
		if(wechatScene!=null){
			
			params.put("scene",wechatScene.getScene());
		}else{
			params.put("scene","场景值没有对应的场景");
		}
		
		List<Map<String, Object>> list=querySceneMessageByDay(params);
		if(list.size()==0){
			
			params.put("count",1);
			insertBiScene(params);
		}else if(list.size()==1){
			
			params.put("count",Integer.parseInt(list.get(0).get("count").toString())+1);
			updateBiScene(params);
		}
		
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-3-15  上午11:48:45
	 * @function <p> 菜单交互 统计  </p>
	 * @param userId
	 * @return
	 */
	public List<Map<String,Object>> queryMenuMessage(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryMenuMessage", params);
	}
	
	/**
	 * 处理wechatmessage表
	 *@author xiaoqian
	 * 2014-5-19下午1:42:56
	 */
	public  void  copyWechatMessage(Map<String,Object> params) {
		daoHelper.queryForObject("com.boxin.ims.bi.copyWechatMessage", params);
		
	}
	
	/**
	 * 奖wechat message 表清理 
	 *@author xiaoqian
	 * 2014-6-17下午2:43:41
	 */
	public void deleteWechatMessage(){
		daoHelper.delete("com.boxin.ims.bi.deleteWechatMessage");
	}
	
	/**
	 * 查询是否创建了本月的数据
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-5-19下午2:22:35
	 */
	public  List<Map<String,Object>>  queryBiDataResult(Map<String,Object> params) {
		return  daoHelper.queryForList("com.boxin.ims.bi.queryBiDataResult", params);
	}
	
	/**
	 * 添加addBiDataResult
	 *@author xiaoqian
	 * 2014-5-19下午2:23:39
	 */
	public  void  addBiDataResult(Map<String,Object> params) {
		daoHelper.insert("com.boxin.ims.bi.addBiDataResult", params);
	}
	
	
	/**
	 * 修改addBiDataResult
	 *@author xiaoqian
	 * 2014-5-19下午2:23:39
	 */
	public  void  updateBIDataResultById(Map<String,Object> params) {
		daoHelper.insert("com.boxin.ims.bi.updateBIDataResultById", params);
	}
	
	
	/**
	 * 添加addBiDataResult
	 *@author xiaoqian
	 * 2014-5-19下午2:23:39
	 */
	public  void  addBiAliveMember(Map<String,Object> params) {
		daoHelper.insert("com.boxin.ims.bi.insertAliveMember", params);
	}
	
	/**
	 * 修改addBiDataResult
	 *@author xiaoqian
	 * 2014-5-19下午2:23:39
	 */
	public  void  updateBIAliveMember(Map<String,Object> params) {
		daoHelper.insert("com.boxin.ims.bi.updateBIAliveMember", params);
	}
	
	
	
	/**
	 * 查询微信信息表
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-9-30下午2:23:18
	 */
	public List<Map<String,Object>> queryMessageByDay(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryMessageByDay", params);
	}

	
	/**
	 * 查询会员表
	 *@author xiaoqian
	 * @param params
	 * @return
	 * 2014-9-30下午5:03:14
	 */
	public List<Map<String,Object>> queryBiWechatMemberByDay(Map<String,Object> params){
		return daoHelper.queryForList("com.boxin.ims.bi.queryBiWechatMemberByDay", params);
	}
	
	
	/**
	 * 删除48小时数据
	 *@author xiaoqian
	 * @param params
	 * 2014-10-28下午5:17:07
	 */
	public void deleteWechatMessageNear(Map<String,Object> params){
		daoHelper.delete("com.boxin.ims.bi.deleteWechatMessageNear", params);
	}
	
}
